﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using ColorApplication.DataAccess;
using ColorApplication.Helper;

namespace ColorApplication.BusinessLayer
{
    public class ColorSayDaoManager
    {
        private static ColorSayDaoManager _instance;

        private ColorSayDaoManager() { }

        public static ColorSayDaoManager Instance
        {
            get { return _instance ?? (_instance = new ColorSayDaoManager()); }
        }

        public List<ColorSayAttribute> GetAttributesFamiliesSelectByTopCount(int topCount)
        {
            var result = new List<ColorSayAttribute>();
            var ds = ColorSayDao.Instance.GetAttributesFamiliesSelectByTopCount(topCount);
            if (ds == null || ds.Tables.Count == 0) return result;
            result = GetAttributesFromDataSet(ds);
            return result;
        }

        private static List<ColorSayAttribute> GetAttributesFromDataSet(DataSet ds)
        {
            var result = new List<ColorSayAttribute>();
            var dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                var att = new ColorSayAttribute { Id = SQLHelper.GetIntValue(row["Id"]), Description = row["Description"].ToString(),
                                                  IsSelectedForOpt = SQLHelper.GetBoolValue(row["IsEnabledForOptions"])
                };
                result.Add(att);
            }
            return result;
        }

        public void UpdateSelectAttrsForOption(List<int> listAttsId)
        {
            var tvpAttrIds = SQLHelper.GenerateDataRecords(listAttsId);
            ColorSayDao.Instance.UpdateSelectAttrsForOption(tvpAttrIds);
        }

        public List<ColorSayAttribute> GetAttributesFamiliesSelectForOption()
        {
            var result = new List<ColorSayAttribute>();
            var ds = ColorSayDao.Instance.GetAttributesFamiliesSelectByTopCount(1111);
            if (ds == null || ds.Tables.Count == 0) return result;
            result = GetAttributesFromDataSet(ds);
            return result;
        }

        public List<string> GetAnalysisUrlForReport(DateTime? fromdate, DateTime? todate)
        {
            var result = new List<string>();
            var ds = ColorSayDao.Instance.GetAnalysisUrlForReport(fromdate, todate);
            if (ds == null || ds.Tables.Count == 0) return result;
            result = GetUrlFromDataSet(ds);
            return result;
        }

        private List<string> GetUrlFromDataSet(DataSet ds)
        {
            var result = new List<string>();
            foreach (DataRow dataRow in ds.Tables[0].Rows)
            {
                result.Add(dataRow["AnalysisUrl"].ToString());
            }
            return result;
        }

        public List<ColorSayAttribute> GetAttributesFamilyForReport()
        {
            var result = new List<ColorSayAttribute>();
            var ds = ColorSayDao.Instance.GetAttributesFamilyForReport();
            if (ds == null || ds.Tables.Count == 0) return result;
            result = GetAttrsFamilyFromDataSet(ds);
            return result;
        }

        private List<ColorSayAttribute> GetAttrsFamilyFromDataSet(DataSet ds)
        {
            var result = new List<ColorSayAttribute>();
            foreach (DataRow dataRow in ds.Tables[0].Rows)
            {
                var atts = new ColorSayAttribute
                               {
                                   Id = SQLHelper.GetIntValue(dataRow["Id"]),
                                   Description = dataRow["Description"].ToString()
                               };
                result.Add(atts);
            }
            return result;
        }
    }
}